package com.threeidiots.findit.db;

import java.util.ArrayList;

import com.threeidiots.findit.db.FindItDatabase.TagsCursor;

/**
 * Entity of Tag field
 * @author Hemslo
 *
 */
public class Tag {
	private FindItDatabase db;
	private long _id;
	private String tag;

	public Tag(FindItDatabase db) {
		this.db = db;
		this._id = 0;
		this.tag = null;
	}
	public Tag(FindItDatabase db, long _id, String tag) {
		this.db = db;
		this._id = _id;
		this.tag = tag;
	}
	
	public void update() {
		this.db.editTag(_id, tag);
	}
	
	public void insert() {
		this.db.addTag(tag);
	}
	
	public void delete() {
		this.db.deleteTag(_id);
	}
	
	public static void clear(FindItDatabase db) {
		db.deleteTag(0);
	}
	
	public static int tagsCount(FindItDatabase db) {
		return db.getTagsCount();
	}
	
	public static ArrayList<Tag> getTags(FindItDatabase db) {
		ArrayList<Tag> tags = new ArrayList<Tag>();
		TagsCursor cursor = db.getTags();
		while (!cursor.isAfterLast()) {
			tags.add(new Tag(
					db,
					cursor.getColTagsId(),
					cursor.getColTagsTag()));
			cursor.moveToNext();
		}
		cursor.close();
		return tags;
	}

	public long getId() {
		return _id;
	}

	public void setId(long _id) {
		this._id = _id;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

}
